package org.clevernote.main;

import java.io.IOException;

import org.clevernote.foundations.Authentication;
import org.clevernote.foundations.EvernoteExtension;
import org.clevernote.foundations.Preferences;
import org.clevernote.foundations.cmd.Cmd;
import org.clevernote.foundations.StringConstants;

import ui.cmdline.parser.CommandLineArguments;

import com.evernote.edam.error.EDAMNotFoundException;
import com.evernote.edam.error.EDAMSystemException;
import com.evernote.edam.error.EDAMUserException;
import com.evernote.edam.notestore.NoteStore;
import com.facebook.thrift.TException;
import com.facebook.thrift.protocol.TBinaryProtocol;
import com.facebook.thrift.transport.THttpClient;
import com.evernote.edam.type.Note;

public class CmdAddNoteStdin implements Cmd {

	public String getName() {
		return StringConstants.CMD_ADD_NOTE_STDIN;
	}

	public void run(CommandLineArguments cmdline, Authentication auth)
			throws TException, EDAMSystemException, EDAMUserException {
		
		THttpClient noteStoreTrans = new THttpClient(auth.getNoteStoreUrl());
		TBinaryProtocol noteStoreProt = new TBinaryProtocol(noteStoreTrans);
		NoteStore.Client noteStore = new NoteStore.Client(noteStoreProt,noteStoreProt);
		
		String authToken = auth.getAuthenticationToken();
		String currNotebook = cmdline.getString(StringConstants.NOTEBOOK,Preferences.retreive(StringConstants.NOTEBOOK));
		String currTitle = cmdline.getString(StringConstants.NOTE_TITLE,Preferences.retreive(StringConstants.NOTE_TITLE));
		String currTags = cmdline.getString(StringConstants.TAGS,Preferences.retreive(StringConstants.TAGS));
		String notebookGuid;
		
		if (currNotebook!=null)
		{
			notebookGuid = EvernoteExtension.getNotebookGuid(currNotebook,noteStore,authToken);
		} else
		{
			notebookGuid = noteStore.getDefaultNotebook(authToken).getGuid();
		}
		
		java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
	    
		StringBuilder noteText = new StringBuilder();
		String nextLine="";
		try {
			nextLine = stdin.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		if (currTitle==null)
		{
			currTitle = new String(nextLine);
		}
		
		while (nextLine!=null)
		{
			noteText.append(nextLine);
			noteText.append("<br/>");
			noteText.append("\n");
			try {
				nextLine = stdin.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		String noteContent = EvernoteExtension.StringToNoteContent(noteText.toString());
		
		Note note = new Note();
		note.setTitle(currTitle);
		note.setCreated(System.currentTimeMillis());
		note.setUpdated(System.currentTimeMillis());
		note.setActive(true);
		note.setNotebookGuid(notebookGuid);
		note.setContent(noteContent);
		Note createdNote = null;
		try {
			createdNote = noteStore.createNote(authToken, note);
		} catch (EDAMNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (createdNote!=null)
		{
			System.out.println("Note created. GUID: " + createdNote.getGuid());
		};
		
		

	}
	
	public static void main(String args[]) {

		try {
			CmdAddNoteStdin cmd = new CmdAddNoteStdin();

			CmdRunner.executeCmd(args, cmd);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
